package com.hdd.biz.account.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hdd.biz.account.entity.Account;
import com.hdd.biz.account.mapper.AccountMapper;
import com.hdd.biz.account.service.AccountBillService;
import com.hdd.biz.account.service.AccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdd.biz.finance.dto.WithdrawVerifyRequest;
import com.hdd.biz.finance.entity.Withdraw;
import com.hdd.biz.finance.service.WithdrawService;
import com.hdd.utils.wx.RedisUtil;
import com.hdd.utils.wx.pay.PayUtil;
import com.hdd.utils.wx.pay.WeixinPay;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 账户表 服务实现类
 * </p>
 *
 * @author fang
 * @since 2021-02-23
 */
@Service
@Slf4j
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService {

    @Autowired
    private AccountBillService accountBillService;
    @Autowired
    private WithdrawService withdrawService;
    @Autowired
    private RedisUtil redisUtil;


    @Override
    public Account findAccount(String userId) {
        QueryWrapper<Account> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public boolean createAccount(String userId) {
        Account account = new Account();
        account.setUserId(userId);
        account.setBalance(new BigDecimal(0.00));
        account.setFrozen(new BigDecimal(0.00));
        account.setIncome(new BigDecimal(0.00));
        return baseMapper.insert(account)>0?true:false;
    }

    @Override
    public boolean increBlance(String id, BigDecimal amount) {
        Account account = baseMapper.selectById(id);
        if(account!=null){
            account.setId(id);
            account.setBalance(account.getBalance().add(amount));
        }
        return baseMapper.updateById(account)>0?true:false;
    }

    @Override
    public boolean reduceBlance(String id, BigDecimal amount) {
        Account account = baseMapper.selectById(id);
        if(account!=null){
            account.setId(id);
            account.setBalance(account.getBalance().subtract(amount));
        }
        return baseMapper.updateById(account)>0?true:false;
    }

    @Override
    public boolean increaseIncome(String id, BigDecimal amount) {
        Account account = baseMapper.selectById(id);
        if(account!=null){
            account.setId(id);
            account.setIncome(account.getIncome().add(amount));
        }
        return baseMapper.updateById(account)>0?true:false;
    }


    @Override
    public boolean increaseFrozen(String id, BigDecimal amount) {
        Account account = baseMapper.selectById(id);
        if(account!=null){
            account.setId(id);
            account.setFrozen(account.getFrozen().add(amount));
        }
        return baseMapper.updateById(account)>0?true:false;
    }

    @Override
    public boolean reduceFrozen(String id, BigDecimal amount) {
        Account account = baseMapper.selectById(id);
        if(account!=null){
            account.setId(id);
            account.setFrozen(account.getFrozen().subtract(amount));
        }
        return baseMapper.updateById(account)>0?true:false;
    }


    @Override
    @Transactional(rollbackFor=Exception.class)
    public boolean withdraw(String id,String userId, String amount, String platforms, String account, String real_name) {
        try {

            if(!reduceBlance(id,new BigDecimal(amount))){
                log.error("提现异常");
                return false;
            };

            if(!increaseFrozen(id,new BigDecimal(amount))){
                log.error("提现异常");
                return false;
            };

            accountBillService.addBill(id,new BigDecimal(amount),"O","提现扣除","0",amount,"0",1);
            int withdrawsId =  withdrawService.insertWithdraws(userId,new BigDecimal(amount),platforms,account,real_name);
            //添加提现记录
            if(Double.parseDouble(amount)<=100){
                WithdrawVerifyRequest request = new WithdrawVerifyRequest();
                request.setId(withdrawsId);
                withdrawService.weiXinVerify(request);
            }

        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            log.error("提现失败");
            return false;
        }
        return true;
    }




}
